package sourceagile.server.githubFileRepository;

import java.io.File;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import sourceagile.server.doclet.featureFile.TokenizeFeatureFile;
import sourceagile.server.doclet.java.ConvertFileContentToJavaDocumentation;
import sourceagile.server.utilities.CreateTempFile;
import sourceagile.server.utilities.FileExtensions;

public class GetGithubClassFile {

	public static JSONObject jsonClassDocument(String username,
			String projectName, String objectSHA, String filePath) {

		String fileContent = null;

		JSONObject classDocument = new JSONObject();

		String fileExtension = FileExtensions.getFileExtension(filePath);

		if (FileExtensions.EXTENSION_JAVA.equals(fileExtension)) {

			fileContent = GetGithubFileContent.getContent(username,
					projectName, objectSHA);

			File file = CreateTempFile.get(fileContent, filePath);

			classDocument = ConvertFileContentToJavaDocumentation
					.loadClassDocumentation(classDocument, file);

		} else if (FileExtensions.EXTENSION_FEATURE.equals(fileExtension)) {

			fileContent = GetGithubFileContent.getContent(username,
					projectName, objectSHA);

			classDocument = TokenizeFeatureFile.getJson(fileContent);
		}

		try {

			classDocument.put("fileContent", fileContent);

		} catch (JSONException e) {

			e.printStackTrace();
		}

		return classDocument;
	}
}
